﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Zhukoff.SolutionPackagerGUI
{
    public partial class FormMain : Form
    {
        private string solutionPackagerPath; //путь к SolutionPackager
        private string solutionPath; //путь к исходному файлу
        private string outputPath; //путь к результату
        private string logFilePath; //путь к результату

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void btnOFDSolPack_Click(object sender, EventArgs e)
        {
            ofd.Title = "Choose SolutionPackager.exe";
            ofd.Filter = "Application (.exe)|*.exe";
            if (DialogResult.OK == ofd.ShowDialog())
            {
                Properties.Settings.Default.SolPackPath = lblSolPackPath.Text = solutionPackagerPath = ofd.FileName;
                Properties.Settings.Default.Save();
            }
            
        }

        private void btnOFDSolution_Click(object sender, EventArgs e)
        {
            ofd.Title = "Choose a solution file";
            ofd.Filter = "ZIP archive (*.zip)|*.zip";
            if (DialogResult.OK == ofd.ShowDialog())
            {
                Properties.Settings.Default.SolutionPath = lblSolutionPath.Text = solutionPath = ofd.FileName;
                Properties.Settings.Default.Save();
            }
        }

        private void btnSFDOutput_Click(object sender, EventArgs e)
        {
            fbd.ShowNewFolderButton = true;
            if (DialogResult.OK == fbd.ShowDialog())
            {
                Properties.Settings.Default.OutputPath = lblOutputPath.Text = outputPath = fbd.SelectedPath;
                Properties.Settings.Default.Save();
            }
        }

        private void btnSFDLogFile_Click(object sender, EventArgs e)
        {
            sfd.Filter = "Log file (*.log)|*.log|Text file (*.txt)|*.txt";
            if (DialogResult.OK == sfd.ShowDialog())
            {
                Properties.Settings.Default.LogFilePath = logFilePath = sfd.FileName;
                Properties.Settings.Default.Save();
            }
        }

        private void btnUnpack_Click(object sender, EventArgs e)
        {
            //создаем список аргументов
            ComandLineArgumentsList args = new ComandLineArgumentsList();
            args.Add(new ComandLineArgument("action", "Extract")); //первый аргумент
            args.Add(new ComandLineArgument("zipfile", "\"" + solutionPath + "\"")); //путь к исходному файлу
            args.Add(new ComandLineArgument("folder", "\"" + outputPath + "\"")); //путь к архиву
            Start(solutionPackagerPath, args.ToString(), true); //запускаем SolutionPackager.exe 
        }

        private void btnPack_Click(object sender, EventArgs e)
        {

        }

        private void Start(string filename, string arguments, bool waitforexit)
        {
            using (Process SPProcess = new Process())
            {
                SPProcess.OutputDataReceived += new DataReceivedEventHandler(SPProcessHandler);

                SPProcess.StartInfo.CreateNoWindow = true;
                SPProcess.StartInfo.UseShellExecute = false;
                SPProcess.StartInfo.ErrorDialog = false;
                SPProcess.StartInfo.RedirectStandardError = true;
                SPProcess.StartInfo.RedirectStandardInput = true;
                SPProcess.StartInfo.RedirectStandardOutput = true;
                //SPProcess.StartInfo.StandardOutputEncoding = Encoding.GetEncoding("UTF-8");
                SPProcess.StartInfo.FileName = filename;
                SPProcess.StartInfo.Arguments = arguments;
                SPProcess.Start();
                SPProcess.BeginOutputReadLine();
                if (waitforexit)
                {
                    SPProcess.WaitForExit();
                }
            }
        }

        void SPProcessHandler(object sender, DataReceivedEventArgs e)
        {
            this.BeginInvoke(new MethodInvoker(() =>
            {
                richtxtOutput.AppendText(e.Data + Environment.NewLine ?? string.Empty);
            }));
        }
    }
}
